package com.asen.commons.core.jdbc.builder.mysql.filter;

import com.asen.commons.core.meta.enums.FilterSymbolEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * 过滤解析处理器
 *
 * @author Gary
 * @version 1.0
 * @date 2021-08-22
 */
public class FilterParserHolder {

    private static final Map<FilterSymbolEnum, FilterParser> PARSER_MAP = new HashMap();

    private static DefaultFilterParser defaultFilterParser = new DefaultFilterParser();
    private static NullFilterParser nullFilterParser = new NullFilterParser();
    private static NumberFilterParser numberFilterParser = new NumberFilterParser();
    private static LikeFilterParser likeFilterParser = new LikeFilterParser();
    private static CustomFilterParser customFilterParser = new CustomFilterParser();
    private static CollectionFilterParser collectionFilterParser = new CollectionFilterParser();

    static {
        PARSER_MAP.put(FilterSymbolEnum.IS_NULL, nullFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.IS_NOT_NULL, nullFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.LIKE, likeFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.NOT_LIKE, likeFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.LEFT_LIKE, likeFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.NOT_LEFT_LIKE, likeFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.RIGHT_LIKE, likeFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.NOT_RIGHT_LIKE, likeFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.IN, collectionFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.NOT_IN, collectionFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.CUSTOM, customFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_EQUAL, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_NOT_EQUAL, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_GREATER, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_GREATER_EQUAL, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_LESS, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_LESS_EQUAL, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_EQUAL_ABS, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_NOT_EQUAL_ABS, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_GREATER_ABS, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_GREATER_EQUAL_ABS, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_LESS_ABS, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_LESS_EQUAL_ABS, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_EQUAL_VALUE, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_NOT_EQUAL_VALUE, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_EQUAL_VALUE_ABS, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.COMPARE_NOT_EQUAL_VALUE_ABS, numberFilterParser);
        PARSER_MAP.put(FilterSymbolEnum.LIKE_IGNORE_CASE, likeFilterParser);
    }

    public static FilterParser get(FilterSymbolEnum filterSymbol) {
        FilterParser filterParser = PARSER_MAP.get(filterSymbol);
        if (filterParser == null) {
            return defaultFilterParser;
        }
        return filterParser;
    }
}
